1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package sun.io;
27
28 import java.io.UnsupportedEncodingException;
29 import java.lang.ref.SoftReference;
30 import java.util.Properties;
31
32
33
34
35
36
37
38
39
40
41
42
43 @Deprecated
44 public class Converters {
45
46 private Converters() { }
47
48
49 private static Object lock = Converters.class;
50
51
52 private static String converterPackageName = null;
53 private static String defaultEncoding = null;
54
55
56 public static final int BYTE_TO_CHAR = 0;
57 public static final int CHAR_TO_BYTE = 1;
58 private static final String[] converterPrefix = { "ByteToChar",
59 "CharToByte" };
60
61
62
63
64 private static final int CACHE_SIZE = 3;
65
66
67 private static final Object DEFAULT_NAME = new Object();
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @SuppressWarnings("unchecked")
89 private static SoftReference<Object[]>[][] classCache
90 = (SoftReference<Object[]>[][]) new SoftReference<?>[][] {
91 new SoftReference<?>[CACHE_SIZE],
92 new SoftReference<?>[CACHE_SIZE]
93 };
94
95 private static void moveToFront(Object[] oa, int i) {
96 Object ob = oa[i];
97 for (int j = i; j > 0; j--)
98 oa[j] = oa[j - 1];
99 oa[0] = ob;
100 }
101
102 private static Class<?> cache(int type, Object encoding) {
103 SoftReference<Object[]>[] srs = classCache[type];
104 for (int i = 0; i < CACHE_SIZE; i++) {
105 SoftReference<Object[]> sr = srs[i];
106 if (sr == null)
107 continue;
108 Object[] oa = sr.get();
109 if (oa == null) {
110 srs[i] = null;
111 continue;
112 }
113 if (oa[1].equals(encoding)) {
114 moveToFront(srs, i);
115 return (Class<?>)oa[0];
116 }
117 }
118 return null;
119 }
120
121 private static Class<?> cache(int type, Object encoding, Class<?> c) {
122 SoftReference<Object[]>[] srs = classCache[type];
123 srs[CACHE_SIZE - 1] = new SoftReference<>(new Object[] { c, encoding });
124 moveToFront(srs, CACHE_SIZE - 1);
125 return c;
126 }
127
128
129
130
131 public static boolean isCached(int type, String encoding) {
132 synchronized (lock) {
133 SoftReference<Object[]>[] srs = classCache[type];
134 for (int i = 0; i < CACHE_SIZE; i++) {
135 SoftReference<Object[]> sr = srs[i];
136 if (sr == null)
137 continue;
138 Object[] oa = sr.get();
139 if (oa == null) {
140 srs[i] = null;
141 continue;
142 }
143 if (oa[1].equals(encoding))
144 return true;
145 }
146 return false;
147 }
148 }
149
150
151
152
153 private static String getConverterPackageName() {
154 String cp = converterPackageName;
155 if (cp != null) return cp;
156 java.security.PrivilegedAction<String> pa =
157 new sun.security.action.GetPropertyAction("file.encoding.pkg");
158 cp = java.security.AccessController.doPrivileged(pa);
159 if (cp != null) {
160
161 converterPackageName = cp;
162 } else {
163
164 cp = "sun.io";
165 }
166 return cp;
167 }
168
169 public static String getDefaultEncodingName() {
170 synchronized (lock) {
171 if (defaultEncoding == null) {
172 java.security.PrivilegedAction<String> pa =
173 new sun.security.action.GetPropertyAction("file.encoding");
174 defaultEncoding = java.security.AccessController.doPrivileged(pa);
175 }
176 }
177 return defaultEncoding;
178 }
179
180 public static void resetDefaultEncodingName() {
181
182 if (sun.misc.VM.isBooted())
183 return;
184
185 synchronized (lock) {
186 defaultEncoding = "ISO-8859-1";
187 Properties p = System.getProperties();
188 p.setProperty("file.encoding", defaultEncoding);
189 System.setProperties(p);
190 }
191 }
192
193
194
195
196
197
198 private static Class<?> getConverterClass(int type, String encoding)
199 throws UnsupportedEncodingException
200 {
201 String enc = null;
202
203
204
205
206
207 if (!encoding.equals("ISO8859_1")) {
208 if (encoding.equals("8859_1")) {
209 enc = "ISO8859_1";
210
211
212
213
214
215
216
217
218
219
220 } else if (encoding.equals("ISO8859-1")) {
221 enc = "ISO8859_1";
222 } else if (encoding.equals("646")) {
223 enc = "ASCII";
224 } else {
225 enc = CharacterEncoding.aliasName(encoding);
226 }
227 }
228 if (enc == null) {
229 enc = encoding;
230 }
231
232 try {
233 return Class.forName(getConverterPackageName()
234 + "." + converterPrefix[type] + enc);
235 } catch(ClassNotFoundException e) {
236 throw new UnsupportedEncodingException(enc);
237 }
238
239 }
240
241
242
243
244
245 private static Object newConverter(String enc, Class<?> c)
246 throws UnsupportedEncodingException
247 {
248 try {
249 return c.newInstance();
250 } catch(InstantiationException e) {
251 throw new UnsupportedEncodingException(enc);
252 } catch(IllegalAccessException e) {
253 throw new UnsupportedEncodingException(enc);
254 }
255 }
256
257
258
259
260
261
262 static Object newConverter(int type, String enc)
263 throws UnsupportedEncodingException
264 {
265 Class<?> c;
266 synchronized (lock) {
267 c = cache(type, enc);
268 if (c == null) {
269 c = getConverterClass(type, enc);
270 if (!c.getName().equals("sun.io.CharToByteUTF8"))
271 cache(type, enc, c);
272 }
273 }
274 return newConverter(enc, c);
275 }
276
277
278
279
280
281
282
283 private static Class<?> getDefaultConverterClass(int type) {
284 boolean fillCache = false;
285 Class<?> c;
286
287
288 c = cache(type, DEFAULT_NAME);
289 if (c != null)
290 return c;
291
292
293 String enc = getDefaultEncodingName();
294 if (enc != null) {
295
296 fillCache = true;
297 } else {
298
299
300 enc = "ISO8859_1";
301 }
302
303
304 try {
305 c = getConverterClass(type, enc);
306 if (fillCache) {
307 cache(type, DEFAULT_NAME, c);
308 }
309 } catch (UnsupportedEncodingException x) {
310
311 try {
312 c = getConverterClass(type, "ISO8859_1");
313 } catch (UnsupportedEncodingException y) {
314 throw new InternalError("Cannot find default "
315 + converterPrefix[type]
316 + " converter class");
317 }
318 }
319 return c;
320
321 }
322
323
324
325
326
327
328 static Object newDefaultConverter(int type) {
329 Class<?> c;
330 synchronized (lock) {
331 c = getDefaultConverterClass(type);
332 }
333 try {
334 return newConverter("", c);
335 } catch (UnsupportedEncodingException x) {
336 throw new InternalError("Cannot instantiate default converter"
337 + " class " + c.getName());
338 }
339 }
340
341 }